package com.zhihuichongcheng.application.ui.im.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.util.DateUtil;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.yunkeshuzhi.common.log.SSLog;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.CustomWebviewActivity;
import com.zhihuichongcheng.application.bean.im.NewsBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 3water on 2016/10/27.
 */
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHold> {
    private Context mContext;
    private List<NewsBean> mListData = new ArrayList<>();
    public NewsAdapter(Context context)
    {
        this.mContext = context;
    }
    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_news,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, int position) {
        final NewsBean bean = mListData.get(position);
        int width = DeviceUtil.getWidth(mContext)-30;
        width = width/2;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,width);
        params.setMargins(DeviceUtil.dip2px(mContext,15),DeviceUtil.dip2px(mContext,15),DeviceUtil.dip2px(mContext,15),0);
        holder.mImage.setLayoutParams(params);
        Glide.with(mContext).load(bean.getMainImgUrl()).placeholder(R.drawable.ic_chat_news_defalut).into(holder.mImage);
        holder.mTvNewsTitle.setText(bean.getTitle());
        holder.mTvNewsContent.setText(Html.fromHtml(bean.getSumary()));
        Date date = DateUtil.parseStringToDate(bean.getCreateTime(),"yyyy年MM月dd日 HH:mm:ss");
        holder.mTvTime.setText(mContext.getResources().getString(R.string.chat_jingbao_news)+"  "+DateUtil.format(date));
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CustomWebviewActivity.class);
                intent.putExtra("title",mContext.getString(R.string.chat_jingbao_news));
//                String url = "http://jingbao-frontend.yirimao.com/#/message-detail?id=";
                String url = "https://web.jm-ccwy.com/#/message-detail?id=";
                intent.putExtra("url", url + bean.getNewsId() + "&type=1");
                SSLog.d("------url----->" + intent.getStringExtra("url"));
                mContext.startActivity(intent);
                ((Activity)mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setData(List<NewsBean> list) {
        this.mListData.addAll(list);
    }

    public void clear() {
        mListData.clear();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private ImageView mImage;
        private View view;
        private TextView mTvNewsTitle,mTvNewsContent,mTvTime;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImage = (ImageView)itemView.findViewById(R.id.im_image);
            mTvNewsTitle = (TextView)itemView.findViewById(R.id.tv_news_title);
            mTvNewsContent = (TextView)itemView.findViewById(R.id.tv_news_content);
            mTvTime = (TextView)itemView.findViewById(R.id.tv_time);
        }
    }
}
